import {
  curTimeAll
} from '../../third-utils/time/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    curCate: '',
    recordDate: '',
    imageAttr: [],
    hxd_type: {},
    typeIndex: '',
    hxd_zz: {},
    zzIndex: '',
    hxd_cxsj: {},
    cxsjIndex: '',
    description: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {


    this.setData({
      curCate: e.cateId,
      recordDate:curTimeAll
    })
    this.initData()
  },


  hideModal() {
    this.setData({
      showModal: false,
    })
  },
  showModal() {
    this.setData({
      showModal: true,
    })
  },
  select(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.currentTarget.dataset.index
    })
  },
  pickerChangeType(e) {
    console.log(e, 21)
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  formSubmit(e) {
    console.log(e, 12122)
    if (this.data.typeIndex == '') {
      wx.showToast({
        title: '请选择类型',
        icon: 'none',
        duration: 1200
      })
      return
    }
    if (this.data.zzIndex == '') {
      wx.showToast({
        title: '请选择症状',
        icon: 'none',
        duration: 1200
      })
      return
    }
    if (this.data.cxsjIndex == '') {
      wx.showToast({
        title: '请选择持续时间',
        icon: 'none',
        duration: 1200
      })
      return
    }
    let post = {
      babyId: 32,
      typeId: this.data.curCate,

    }
    post.detail = {
      recordDate: this.data.recordDate,
      type: this.data.hxd_type.opts[this.data.typeIndex].name,
      symptom: this.data.hxd_zz.opts[this.data.zzIndex].name,
      frequency: this.data.hxd_cxsj.opts[this.data.cxsjIndex].name,
      sverity: 7,
      description: e.detail.value.description,
      picture: String(this.data.imageAttr)
    }
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data: post,
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {

          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  changeCate(e) {
    this.setData({
      curCate: e.currentTarget.dataset.index
    })
  },
  delImg(e) {
    this.data.imageAttr.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      imageAttr: this.data.imageAttr
    })
  },
  addImg() {
    wx.chooseImage({
      count: 9,
      success: (chooseImageRes) => {
        console.log(chooseImageRes)
        const tempFilePaths = chooseImageRes.tempFilePaths;
        tempFilePaths.some((item) => {
          wx.uploadFile({
            url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
            filePath: item,
            fileType: 'image',
            name: 'file',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'multipart/form-data',
            },
            success: (uploadFileRes) => {
              let res = JSON.parse(uploadFileRes.data)
              if (res.code == 200) {
                this.data.imageAttr.push(res.data.imageUrl)
                this.setData({
                  imageAttr: this.data.imageAttr
                })
              }
            },
          });
        })
      }
    });
  },
  initData() {
    let codeAttr = ['hxd_type', 'hxd_zz', 'hxd_cxsj']
    codeAttr.some(item => {
      wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
        data: {
          code: item
        },
        method: 'get',
        success: (res) => {
          if (res.data.code == 200) {
            this.setData({
              [item]: res.data.data
            })
          }
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})